package com.yunhe.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.javabean.Orders;
import com.yunhe.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit,@RequestParam(value = "key",required = false,defaultValue = "")String key,@RequestParam(value = "value",required = false,defaultValue = "") String value){
        PageHelper.startPage(page,limit);
        List<Orders> all =null;
       switch (key){
           case "orderNum":
              all= ordersService.findAll(value,null);
              break;
           case "productName":
               all= ordersService.findAll(null,value);
               break;
           case "":
                all= ordersService.findAll(null,null);
       }
        request.setAttribute("select",key);
        request.setAttribute("selectValue",value);

        PageInfo<Orders> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/orders/orders-list";
    }

    @RequestMapping("/findById")
    public String findById(HttpServletRequest request,int id){
        Orders orders = ordersService.findById(id);
        request.setAttribute("order",orders);
        return "/orders/orders-show";
    }





}
